Linking Word 2007 Content Controls to Custom XML

Andrew Coates [MSFT]

One of the demos I showed in my session at OzFox was inserting some new Custom XML into an existing template file and having that bound to some content controls. This makes mail merges REALLY easy. It also allows you to pick up changes to customer details made in the document itself and propagate them back to the server, because the binding between the Content Controls and the Custom XML is dynamic and bi-directional.

However, doing the binding in the first place is not all that intuitive. Here is a series of steps that worked for me - note that I'm not saying it's the only, or even the best way.

BTW, there's a great video up on Channel 9 where Matthew Scott discusses the Word 2007 Content Control Toolkit.

Step 1: Download and install the Word 2007 Content Control Toolkit from CodePlex.

Grab the installer, the documentation and (optionally) the source code from here.

Run the MSI file and accept the defaults.

Step 2: Create a Word 2007 document to use as a template

It doesn't really matter what's in it, but you can add some placeholder text using the =rand() function if you like.

Step 3: Add Content Controls to the document

You'll need to enable the developer tab on the ribbon, then just drag controls from the controls group into the document:

I used text controls (second from the left in the top row), but you can get fancier with Rich Text, images, combo boxes, datetime pickers etc.

Highlight each control and click the properties button in the controls group of the developer toolbar.

Properties button

Give the control a sensible name.

Note that there are a number of other options for the control's properties. If you want the bi-directional data binding to work, make sure you don't check the "Remove content control when contents are edited" box.

Do this for as many fields as you want to bind to.

Save and close the document

Step 4: Add some custom XML to the document

Open the document using the Word 2007 Content Control Toolkit.

Word document open in the content control toolkit

Note that at this stage there's no custom XML in the document. Click the Create a new Custom XML Part hyperlink and change to the Edit View tab and add some XML. You don't need a schema, it just needs to be well-formed XML. Here's the XML I used:

<root>
<name>
<givenName>Charles</givenName>
<familyName>Sterling</familyName>
<salutation>Chuck</salutation>
</name>
<address>
<location>Level 9</location>
<street>1 Eagle St</street>
<suburb>BRISBANE</suburb>
<state>QLD</state>
<postalCode>4000</postalCode>
<country>AUSTRALIA</country>
</address>
</root>

Custom XML inserted 

Step 5: Bind the Content Controls to custom XML nodes

Go back to Bind View and drag nodes from the tree on the right to the appropriate control on the left. Note that you need to select the node before you drag it.

Binding the

Save the document and open it in Word.

Document with data from Custom XML

Now change something (for example change the salutation from "Chuck" to "Charles"). Save and close the file and open it using the Content Control Toolkit. Notice that the XML has been changed.